package com.ITArmy.service.impl;

import com.ITArmy.beans.Type;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.TypeMapper;
import com.ITArmy.service.TypeService;
import com.ITArmy.util.CheckMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    /**
     *遍历类型
     */
    @Override
    public ServerResponse typeList() {
        List<Type> types = typeMapper.selectAllType();
        if (types != null) {
            return ServerResponse.createBySuccess(types);
        } else {
            return ServerResponse.createByErrorMesage("无相关内容！");
        }
    }

    /**
     * 添加类型
     */
    @Override
    public ServerResponse addType(Type type) {
        //类型查重
        Type t = typeMapper.selectByName(type.gettName());
        if (t != null) {
            return ServerResponse.createByErrorMesage("该类型已存在");
        } else {
            int resultCount = typeMapper.insertSelective(type);
            CheckMessage check=new CheckMessage();
            return check.getMessage(resultCount,"添加成功","添加失败");
        }
    }

    /**
     *删除类型
     */
    @Override
    public ServerResponse deleteType(Long id) {
        int resultCount = typeMapper.deleteByPrimaryKey(id);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    /**
     *批量删除
     */
    @Override
    public ServerResponse deleteManyByIds(String[] ids) {
        int resultCount = typeMapper.deleteManyByIds(ids);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    /**
     *修改类型
     */
    @Override
    public ServerResponse updateType(Type type) {
        int resultCount = typeMapper.updateByPrimaryKeySelective(type);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "修改成功", "修改失败");
    }

    /**
     *查找类型
     */
    @Override
    public ServerResponse findTypeByNameLike(String name) {
        List<Type> types = typeMapper.selectByNameLike(name);
        if (types != null) {
            return ServerResponse.createBySuccess(types);
        } else {
            return ServerResponse.createByErrorMesage("无该类型信息");
        }
    }

    /**
     *类型查重
     */
    @Override
    public ServerResponse findTypeByName(String name) {
        Type type = typeMapper.selectByName(name);
        if (type != null) {
            return ServerResponse.createBySuccess(type);
        } else {
            return ServerResponse.createByErrorMesage("无该类型信息");
        }
    }
}
